package character.sprite.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import character.sprite.model.CharacterSprite;
import data.ObjectDatabase;

public class CharacterSpriteBuilder extends JDialog implements ActionListener,
		WindowListener {

	private static final long serialVersionUID = -5771875967789692858L;

	private CharacterStyleListPanel styleListPanel;
	private CharacterStyleDropZonePanel styleDropZonePanel;
	private CharacterSpriteAnimator spriteAnimator;
	private JButton importStyleButton, changeSpriteName;
	private String charSpriteClassName, spriteGUID;

	public CharacterSpriteBuilder(JFrame parent, String spriteGUID) {
		super(parent, "Editing Sprite");

		this.setSize(450, 400);
		this.setModal(true);
		this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		this.setLocation(this.getWidth(), this.getHeight());
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLayout(new BorderLayout());

		charSpriteClassName = CharacterSprite.class.getCanonicalName();

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		importStyleButton = new JButton("Import Styles");
		importStyleButton.addActionListener(this);
		changeSpriteName = new JButton("Change Sprite Name");
		changeSpriteName.addActionListener(this);
		buttonPanel.add(importStyleButton);
		buttonPanel.add(changeSpriteName);

		styleListPanel = new CharacterStyleListPanel();
		styleDropZonePanel = new CharacterStyleDropZonePanel(spriteGUID);

		if (spriteGUID != null) {
			this.spriteGUID = spriteGUID;
			this.setTitle("Editing Sprite: \""
					+ ObjectDatabase.getInstance().getEditorObject(
							charSpriteClassName, spriteGUID).getData()
							.getDisplayName() + "\"");
		} else {
			this.spriteGUID = ObjectDatabase.getInstance().addEditorObject(
					charSpriteClassName).getGUID();
			this.changeSpriteName();
		}

		spriteAnimator = new CharacterSpriteAnimator(225, 250, this.spriteGUID);

		this.add(styleListPanel, BorderLayout.WEST);

		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
		centerPanel.add(styleDropZonePanel, BorderLayout.NORTH);
		centerPanel.add(spriteAnimator, BorderLayout.CENTER);
		this.add(centerPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}

	private void changeSpriteName() {
		boolean done = false;
		String input = "New " + CharacterSprite.class.getName();
		while (!done) {
			input = JOptionPane.showInputDialog(this,
					"Enter a name for this sprite", "My Sprite",
					JOptionPane.INFORMATION_MESSAGE);
			if (input != null
					&& !ObjectDatabase.getInstance().dataObjectExistsOfName(
							charSpriteClassName, input)) {
				done = true;
			}
		}
		ObjectDatabase.getInstance().getEditorObject(charSpriteClassName,
				spriteGUID).getData().setDisplayName(input);
		this.setTitle("Editing Sprite: \"" + input + "\"");
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JButton source = (JButton) arg0.getSource();
		if (source.equals(importStyleButton)) {
			CharacterStyleDeploymentTool importTool = new CharacterStyleDeploymentTool(
					this);
			importTool.setVisible(true);
		} else if (source.equals(changeSpriteName)) {
			this.changeSpriteName();
		}
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		spriteAnimator.stopAnimation();
		this.dispose();
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
	}

}
